package com.orhon.smartcampus.modules.student.fetcher;

import java.util.HashMap;
import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import com.orhon.smartcampus.basicComponents.Result;
import com.orhon.smartcampus.modules.base.gqlModel.GQLPageParam;
import com.orhon.smartcampus.modules.base.mm.EnvArgsMapper;
import com.orhon.smartcampus.modules.student.dao.HonorDao;
import com.orhon.smartcampus.modules.student.entity.Honor;
import com.orhon.smartcampus.modules.student.gqlModel.GQLHonor;
import com.orhon.smartcampus.modules.student.mm.HonorMapper;
import com.orhon.smartcampus.modules.user.dao.TokenService;
import com.orhon.smartcampus.utils.ObjectToMap;
import com.orhon.smartcampus.utils.Util;

import graphql.schema.DataFetcher;
import graphql.schema.DataFetchingEnvironment;


@Component
public class HonorsFetcher implements DataFetcher {
	@Autowired
	HonorDao honorDao;

	@Autowired
	TokenService tokenService;

	@Override
	public Object get(DataFetchingEnvironment env) throws Exception {

		GQLPageParam page = EnvArgsMapper.MAPPER.fromArgs(env);
		String hql = "from Honor where deleted_at = null ";
		if(env.containsArgument("condition")) {
			HashMap<String,Object> map = ObjectToMap.to(env.getArgument("condition"));
			if(map.get("status")!=null) {
				hql += " and status  " + Util.decide(map.get("status"));
			}
			if(map.get("id")!=null) {
				hql += " and id  " + Util.decide(map.get("id"));
			}
			if(map.get("student_id")!=null) {
				hql += " and student_id  " + Util.decide(map.get("student_id"));
			}
			hql += " order by id desc";
		}
		List<Honor> list = honorDao.find(hql,null,page.getPage(),page.getLimit());

		List<GQLHonor> list1 = HonorMapper.MAPPER.toHonorList(list);
		Long total = 0L;
		if (env.getSelectionSet().contains("total")){
			total = honorDao.getTotalCount(hql);
		}
		return Result.ok().put("data", list1).put("total", total);
	}



}
